<template>
  <div class="role-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- <span>筛选搜索</span> -->
        <div class="action">
          <el-button @click="resetCondition">重置</el-button>
          <el-button :loading="isLoading" @click="searchResource" type="primary"
            >查询搜索</el-button
          >
        </div>
        <div class="search">
          <div>
            <span>资源名称：&emsp;</span>
            <el-input
              placeholder="资源名称"
              v-model="conditionObj.name"
              clearable
            >
            </el-input>
          </div>
          <div>
            <span>资源路径：&emsp;</span>
            <el-input
              placeholder="资源路径"
              v-model="conditionObj.url"
              clearable
            >
            </el-input>
          </div>
          <div>
            <span>资源分类：&emsp;</span>
            <el-select placeholder="资源名称" v-model="conditionObj.categoryId">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <el-button class="mt30" @click="openDialog">添加资源</el-button>
      </div>
      <el-table :data="resources" style="width: 100%">
        <el-table-column prop="id" label="编号" width="180"> </el-table-column>
        <el-table-column prop="name" label="资源名称" width="180">
        </el-table-column>
        <el-table-column prop="url" label="资源路径" width="180">
        </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column prop="createdTime" label="添加时间"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button @click="editResource(scope.row)" type="text"
              >编辑</el-button
            >
            <el-button @click="deleteResource(scope.row)" type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 资源新增或边界对话框结构 -->
    <el-dialog
      :title="isEdit ? '编辑资源' : '新增资源'"
      :visible.sync="dialogFormVisible"
      width="30%"
      @close="resourceId = ''"
    >
      <!-- 新增或编辑功能单独封装 -->
      <save-or-edit
        :is-edit="isEdit"
        :resource-id="resourceId"
        @success="onSuccess"
        @cancel="onCancel"
      ></save-or-edit>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAllCategory,
  deleteResource,
  getResourcePages
} from '@/service/resource'
import SaveOrEdit from './save-or-edit.vue'
export default {
  components: { SaveOrEdit },
  name: 'ResourceList',
  data () {
    return {
      options: [],
      isLoading: false,
      roleName: '',
      conditionObj: {
        current: 1,
        size: 10,
        name: '',
        url: '',
        categoryId: ''
      },
      // 当前页
      currentPage: 1,
      pageSize: 10,
      // 资源总数
      total: 0,
      resources: [
        {
          id: 1,
          createdBy: 'system',
          updatedBy: 'system',
          createdTime: '2021-05-19T01:44:08.000+0800',
          updatedTime: '2021-05-19T01:44:08.000+0800',
          operatorId: null,
          code: 'ADMIN',
          name: '超级管理员',
          description: '后台管理员，初始拥有权限管理功能'
        }
      ],
      isEdit: false,
      // 资源新增或编辑弹窗是否显示
      dialogFormVisible: false,
      resourceId: ''
    }
  },
  created () {
    this.loadAllCategory()
    this.loadResource()
  },
  methods: {
    // 新增或编辑资源操作成功
    onSuccess () {
      // 操作完成后资源id初始化
      this.resourceId = ''
      this.dialogFormVisible = false
      this.loadResource()
    },
    // 新增或编辑资源取消操作
    onCancel () {
      // 操作完成后资源id初始化
      this.resourceId = ''
      this.dialogFormVisible = false
      // this.loadAllRole()
    },
    // 打开资源新增或编辑对话框
    openDialog () {
      this.isEdit = false
      this.dialogFormVisible = true
    },
    // 编辑
    editResource (row) {
      this.resourceId = row.id
      this.isEdit = true
      this.dialogFormVisible = true
    },
    // 删除资源
    deleteResource (row) {
      //   console.log(row)
      this.$confirm('即将删除该资源, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data } = await deleteResource(row.id)
          if (data.code === '000000') {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.loadAllRole()
          }
        })
        .catch(err => {
          if (err && err.response) {
            this.$message({
              type: 'error',
              message: '操作失败'
            })
          } else {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          }
        })
    },
    // 重置搜索条件
    resetCondition () {
      // 搜索条件初始化
      this.conditionObj.name = ''
      this.conditionObj.url = ''
      this.conditionObj.categoryId = ''
      this.conditionObj.current = 1
      this.loadResource()
    },
    async loadResource () {
      const { data } = await getResourcePages(this.conditionObj)
      if (data.code === '000000') {
        // this.$message.success('查询成功')
        console.log(data.data)
        this.isLoading = false
        this.resources = data.data.records ?? []
        this.currentPage = data.data.current
        this.total = data.data.total
      }
      this.isLoading = false
    },
    // 查询资源
    async searchResource () {
      this.isLoading = true
      // 页码重置
      this.conditionObj.current = 1
      // this.conditionObj.size = 10
      this.loadResource()
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      // 每页数量变化时页码初始化置1
      this.conditionObj.current = 1
      this.conditionObj.size = val
      this.loadResource()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.conditionObj.current = val
      this.loadResource()
    },
    async loadAllCategory () {
      const { data } = await getAllCategory()
      if (data.code === '000000') {
        this.options = data.data
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.mt30 {
  margin-top: 30px;
}
.clearfix:after {
  clear: both;
}

.box-card {
  text-align: left;
  .action {
    float: right;
    .el-button {
      margin-left: 16px;
    }
  }
  .search {
    display: flex;
    align-items: center;
    margin-top: 30px;
    > div {
      // display: flex;
      margin-left: 20px;
      > span {
        width: 100px;
      }
      .el-input {
        width: 200px;
      }
    }
  }
  .el-pagination {
    margin-top: 20px;
  }
}
</style>
